package com.utils;


import org.apache.axiom.util.base64.Base64Utils;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.regex.Pattern;

/**
 * <p>
 * 自定义 Base64 工具类
 * </p>
 * <p>
 * Copyright:.All rights reserved.
 * </p>
 * <p>
 * Company:Zsoft
 * </p>
 * <p>
 * CreateDate: 2022-08-16
 * </p>
 *
 * @author YuGongWen
 * @history Mender: YuGongWen；Date: 2022-08-16；
 */
public class MyBase64Utils {



    /**
     * 获取字符串的 Base64 字符串
     * 由于【+】会被系统拦截器替换为空格，所以【+】会提前替换为 28343746
     * 由于【=】会被系统拦截器替换为空格，所以【=】会提前替换为 87284389
     * 由于【/】会被系统拦截器替换为空格，所以【/】会提前替换为 09903904
     *
     * @param inputStr
     * @return
     */
    public static String getBase64Str(String inputStr) {
        if (StringUtils.isBlank(inputStr)) {
            return inputStr;
        }

        String base64Str = Base64Utils.encode(inputStr.getBytes(StandardCharsets.UTF_8));

        base64Str = StringUtils.replace(base64Str, "+", "28343746");
        base64Str = StringUtils.replace(base64Str, "=", "87284389");
        base64Str = StringUtils.replace(base64Str, "/", "09903904");
        return base64Str;
    }


    /**
     * 解码 Base64 字符串
     * 由于【+】会被系统拦截器替换为空格，所以【+】会提前替换为 28343746
     * 由于【=】会被系统拦截器替换为空格，所以【=】会提前替换为 87284389
     * 由于【/】会被系统拦截器替换为空格，所以【/】会提前替换为 09903904
     *
     * @param base64Str
     * @return
     */
    public static String getStrByBase64(String base64Str) {
        if (StringUtils.isBlank(base64Str)) {
            return base64Str;
        }

        if (ifBase64Str(base64Str)) {
            // nothing to do
        } else {
            // 非 Base64 格式，不做处理，直接返回
            return base64Str;
        }
        base64Str = StringUtils.replace(base64Str, "28343746", "+");
        base64Str = StringUtils.replace(base64Str, "87284389", "=");
        base64Str = StringUtils.replace(base64Str, "09903904", "/");

        byte[] decode = Base64Utils.decode(base64Str);
        return new String(decode, StandardCharsets.UTF_8);
    }

    /**
     * 判断字符串是否为 Base64 格式
     *
     * @param str
     * @return
     */
    public static boolean ifBase64Str(String str) {
        if (StringUtils.isBlank(str)) {
            return false;
        }
        String base64Pattern = "^([A-Za-z0-9+/])*([A-Za-z0-9+/]|[A-Za-z0-9+/]=|[A-Za-z0-9+/])$";
        if (Pattern.matches(base64Pattern, str)) {
            return true;
        } else {
            return false;
        }
    }


    public static void main(String[] args) {
        String base64String = "eJx9l/s/E47/xecWahbLZSGXt/AezdwvS+5jLi23uRS5Jre5TEwmlIix3FIypVxymxpj5PrWXMNyyeSuELmTa3rz7fPT9/P4/vB9nsfrcR7ncV7/wLG25OAUBPDw8AAIFocOgP9C7M+pKP9BRVNDU1NHWVPDTVVZVU1ZU1VL6U7knQ6X3sc3ugSmFhWrzwLv4HcrnkYDdaKZZGNcPjzp2fF5ad68f8jtDy6106UUq81iDFmoDHbITT+V+xyt/NqGech0O3yR0yphsborDPvxpcpUh9y1qbdvERhXWMS739+ELgTao6FN0cfGT5vRSHjztlrlphlh8YoiPIykWtS01WnQDllaDa4F1bVIOqw+qH6ZbPTwgFqnyuEZ9mUCu4aWprH8r0EYjtp3f7bZlNDCQ6TyEa48ood7ItZ6J22t8JNtR7hISGbdyH2QxRwGZSjCIKowxLCHyIEEFEE3wwaCyF2I0wwwvJHdVCmhrrOC+lzDbNVOehZYS4z8Eiqk/tCBu1/3pdV4fOCAyKuYlflByYVCtw9wwq/lxtiTQ9I1yZbwX8s9JXV69XevLxUOpZWOdhsKKMA5EjFFszBueoWoYREsNAdt9m/M0zX1FmDcrEIgN4Nlu3Pfvsqi4hUsa/GKmq9Yo0D8vncNGtuVpdlZxn+v5vXBKxEDY18+rtoccQ4CyNcx4LKW5MupylqJe3pjC+S0us5DTHET60uZePZgp9cw4gWF67uTFxB+4/GzqYqgZG433YP1Nnx/0Gk3rBuE/jEzhSqbv8SZMbe049l0OWdxySSIdDdhRGtEBjTu52aa6lHc0fAgJVlLKGOfuBijPt1vNHmgCWJNtJOj1pJMXhVs5womcNbuyp1yyCrL30folL7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EchPVtN64RRJ14RH4ob0V/Zn5jC1fc2iTRGgOUnNwiZAHtdkOhN4Ci3NzGOAEdnSToQ8o99RcqYU/J72DFsccLJHiweq7Tee1cYgVKlULMqkmXc0hA8Syh9ZfXSOpz56HQIgwS6JlvySr2vlzBWROvt+lUNCflvas08nKr+e7ehFlrUIT0/Buj9+lKDDlRkSst8oPm6DftX/0ZHEyfsNqVkDI7PcAromKrqagkBcpzA6fWyq+igm8cv+Bdp6HFO5b4GVcEXtgKSWlVZKM4y+I3vEF6/cuV8a6N5Hu5UVvQvnj8zMkNGmXMN3Jiu4N5G4nEUgTvgacvhREZrc2Pg87+PGHeP/M5amV+XQx/8tOY3n4CsLbk5hFRakGVAAEAghngT2ZjFwT87+L5704M8H/5f/aPtSXXqf+8sP2R+R/fBP4n/Q8Y5VGN";

        String str= getStrByBase64( base64String);
        System.out.println(str);

    }


}
